package com.tanhua.app.controller;

import com.tanhua.app.Service.UserInfoService;
import com.tanhua.app.Service.UserService;
import com.tanhua.app.interceptor.UserThreadLocal;
import com.tanhua.model.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据手机号获取验证码
     * @param map
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userService.sendMessage(phone);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        String verificationCode = (String) map.get("verificationCode");
        Map resultMap = userService.loginVerification(phone, verificationCode);
        return ResponseEntity.ok(resultMap);
    }

    /*  03 首次登录---完善资料*/
    //POST/user/loginReginfo
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userinfo) {
        Long id = UserThreadLocal.getid();
        userinfo.setId(id);
        userInfoService.save(userinfo);

        return ResponseEntity.ok(null);
    }

    //04 首次登录---补充头像
    //POST/user/loginReginfo/head
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head (MultipartFile headPhoto ) throws IOException {
        Long id = UserThreadLocal.getid();
        userInfoService.updateHead(headPhoto,id);
        return ResponseEntity.ok(null);

    }
}
